﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using OpenSunTracker.RestClient;
using System.Collections.ObjectModel;
using System.Reflection;
using OSTWin.Properties;

namespace OSTWin
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        #region CTOR
        public MainWindow()
        {
            Client = new OpenSunTrackerClient();

            InitializeComponent();
        }
        #endregion

        #region Properties

        public String AppTitle
        {
            get
            {
                var assName = Assembly.GetExecutingAssembly().GetName();

                var title = assName.Name + " v." + assName.Version.ToString();

                if (Client.HasConfig)
                {
                    title += " - " + Client.Config.Name;
                }
                return title;
            }
        }

        public string CurrentUrl { get { return txtTargetAddress.Text; } }

        public OpenSunTrackerClient Client { get; private set; }

        #endregion

        #region EventHandler
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                txtTargetAddress.Text = Settings.Default.LastUrl;

                //btnConnect_Click(null, null);

                DataContext = this;

            }
            catch (Exception ex)
            {
                Client.AddMessage(ex.Message);
            }
        }

        private void btnConnect_Click(object sender, RoutedEventArgs e)
        {
            try
            {

                if (String.IsNullOrEmpty(CurrentUrl))
                    throw new Exception("no url definied");

                var currentUrl = CurrentUrl;
                if (currentUrl.EndsWith("/"))
                {
                    currentUrl = CurrentUrl.Remove(currentUrl.Length - 1, 1);
                }
                Client.Url = currentUrl;

                UpdateState();

                Settings.Default.LastUrl = Client.Url;
                Settings.Default.Save();

                Client.AddMessage("connection2: " + Client.Url);
            }
            catch (Exception ex)
            {
                Client.AddMessage(ex.Message);
            }
        }

        private void tabMain_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (e.AddedItems.Count == 0)
                    return;

                var selectedTab = e.AddedItems[0] as TabItem;

                if (selectedTab == null)
                    return;

                if (selectedTab.Header.ToString() == "Log" && !Client.HasLogs)
                {
                    UpdateLogs();
                }
  
                if (selectedTab.Header.ToString() == "Configuration" && !Client.HasConfig)
                {
                    UpdateConfiguration();
                }

            }
            catch (Exception ex)
            {
                Client.AddMessage(ex.Message);
            }
        }

        private void UpdateConfiguration()
        {
            Client.UpdateConfiguration();
        }

        private void UpdateLogs()
        {
            Client.UpdateLogs();
        }

        private void btnUpdateRss_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                UpdateLogs();
            }
            catch (Exception ex)
            {
                Client.AddMessage(ex.Message);
            }
        }

        private void btnUpdateConfig_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                UpdateConfiguration();
            }
            catch (Exception ex)
            {
                Client.AddMessage(ex.Message);
            }
        }

        private void btnUpdateState_Click(object sender, RoutedEventArgs e)
        {

            try
            {
                UpdateState();
            }
            catch (Exception ex)
            {
                Client.AddMessage(ex.Message);
            }
        }

        private void UpdateState()
        {
            Client.UpdateState();

            ucState.UpdateVisuals();
            ucTrackerMain.UpdateVisuals();

        }

        #endregion
    }
}
